<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="name"><el-input v-model="queryParams.name" placeholder="请输入节目名称" clearable size="small" @keyup.enter.native="handleQuery"/></el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
<el-col :span="1.5"><el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd" v-hasPermi="['iot:hgitems:add']">新增</el-button></el-col>
<el-col :span="1.5"><el-button type="success" plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate" v-hasPermi="['iot:hgitems:edit']">修改</el-button></el-col>
<el-col :span="1.5"><el-button type="danger" plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete" v-hasPermi="['iot:hgitems:remove']">删除</el-button></el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/></el-row>
    <el-table v-loading="loading" :data="hgitemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center"/>
      <el-table-column label="客户" align="center" prop="nickName" width="155"/>
      <el-table-column label="项目" align="center" prop="projectname" width="155"/>
      <el-table-column label="节目编号" align="center" prop="id" />
      <el-table-column label="节目名称" align="center" prop="name" />
      <el-table-column label="宽" align="center" prop="width" />
      <el-table-column label="高" align="center" prop="height" />
      <el-table-column label="字体大小" align="center" prop="fsize" />
      <el-table-column label="文字内容" align="center" prop="remark" width="161" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="156">
        <template slot-scope="scope">{{scope.row.createTime}}</template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="156">
        <template slot-scope="scope">{{scope.row.updateTime||'未更新'}}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="108">
        <template slot-scope="scope">
  <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['iot:hgitems:edit']">修改</el-button>
  <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['iot:hgitems:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" :page-sizes="pageSizes" @pagination="getList"/>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <div class="scrollable-content">
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
        <el-form-item label="客户名称" prop="customerId">
          <el-select v-model="form.customerId" placeholder="请选择客户名称" filterable style="width:100%" @change="handleNicknameChange">
            <el-option v-for="item in options" :key="item.userId" :label="item.nickName" :value="item.userId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择项目名称" filterable style="width:100%" @change="handleProjectnameChange">
            <el-option v-for="item in projectnameOptions" :key="item.id" :label="item.projectname" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="节目名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入节目名称" />
        </el-form-item>
        <el-form-item label="宽" prop="width">
          <el-input-number v-model="form.width" placeholder="请输入宽" :min="0"/>
        </el-form-item>
        <el-form-item label="高" prop="height">
          <el-input-number v-model="form.height" placeholder="请输入高" :min="0"/>
        </el-form-item>
        <el-form-item label="图片组" prop="images">
          <fileUploadd v-model="form.images" style="width:100%"/>
        </el-form-item>
        <el-form-item label="图片播放时间" prop="imgPTime">
          <el-input-number v-model="form.imgPTime" placeholder="单张图片播放时间" :min="0"/>
        </el-form-item>
        <el-form-item label="视频">
          <fileUploadd v-model="form.attachfile" style="width:100%"/>
        </el-form-item>
        <el-form-item label="字体大小" prop="fsize">
          <el-input-number v-model="form.fsize" placeholder="请输入字体大小" />
        </el-form-item>
        <el-form-item label="文字内容" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入文字内容" />
        </el-form-item>
        <el-form-item label="文字播放时间" prop="textPTime">
          <el-input-number v-model="form.textPTime" placeholder="请输入文字播放时间" :min="0"/>
        </el-form-item>
      </el-form>
      </div>
<div slot="footer" class="dialog-footer"><el-button type="primary" @click="submitForm">确 定</el-button><el-button @click="cancel">取 消</el-button></div>
    </el-dialog>
  </div>
</template>
<script>
import { listHgitems, getHgitems, delHgitems, addHgitems, updateHgitems } from "@/api/iot/hgitems";
import { listUser } from '@/api/iot/tool'
import { listProject } from '@/api/iot/faproject'
export default {
  name: "Hgitems",
  data() {
    return {
      pageSizes: [10,20,30,50],
      options: [],
      projectnameOptions: [],
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      hgitemsList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adminId: null,
        customerId: null,
        projectId: null,
        name: null,
        type: null,
        width: null,
        height: null,
        fsize: null,
        json: null,
        refreshtime: null,
        createtime: null,
        updatetime: null,
        deletetime: null,
        weigh: null,
        switch: null,
        state: null,
        lids: null,
        images: null,
        attachfile: null,
        textPTime: null,
        imgPTime: null,
        mp4PTime: null
      },
      form: {},
      rules: {
        customerId: [{ required: true, message: "客户名称不能为空", trigger: "blur" }],
        projectId: [{ required: true, message: "项目名称不能为空", trigger: "blur" }],
        name: [{ required: true, message: "节目名称不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
    this.fetchUsers();
    this.fetchProjectnames();
  },
  methods: {
    async fetchUsers() {
      try {
        const response = await listUser();
        var resList=response.rows;
        if (response && resList) {
          this.options = resList.map(user => ({
            userId: user.userId,
            nickName: user.nickName
          }));
        }
      } catch (error) {
        console.error('1', error);
      }
    },
    async fetchProjectnames() {
      try {
        const response = await listProject();
        var resList=response.rows;
        if (response && resList) {
          this.projectnameOptions = resList.map(item => ({
            id: item.id,  // 用于存储的项目 ID
            projectname: item.projectname,  // 用于展示的项目名称
          }));
          console.log("2", this.projectnameOptions);
        } else {
          console.error('Invalid response format:', response);
        }
      } catch (error) {
        console.error('Error fetching project names:', error);
      }
    },
    handleNicknameChange(selectedId) {
      const selectedOption = this.options.find(option => option.userId === selectedId);
      if (selectedOption) {
        this.form.customerId = selectedOption.userId;    // 将选择的 userId 存入 adminId 字段
      }
    },
    handleProjectnameChange(selectedId) {
      const selectedOption = this.projectnameOptions.find(option => option.id === selectedId);
      if (selectedOption) {
        this.form.projectId = selectedOption.id;
      } else {
        console.error('No matching data found for value:', value);
      }
    },
    getList() {
      this.loading = true;
      listHgitems(this.queryParams).then(response => {
        this.hgitemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        customerId: null,
        projectId: null,
        id: null,
        adminId: null,
        name: null,
        type: null,
        width: null,
        height: null,
        fsize: null,
        remark: null,
        json: null,
        refreshtime: null,
        createtime: null,
        updatetime: null,
        deletetime: null,
        weigh: null,
        switch: null,
        state: null,
        lids: null,
        images: null,
        attachfile: null,
        textPTime: 10,
        imgPTime: 10,
        mp4PTime: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHgitems(id).then(response => {
        this.form = response.data;
        console.log("3", response.data);
        this.open = true;
        this.title = "修改";
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHgitems(this.form).then(response => {
              if(response != undefined){
                this.$modal.msgSuccess("修改成功");
              }
              this.open = false;
              this.getList();
            });
          } else {
            addHgitems(this.form).then(response => {
              if(response != undefined){
                this.$modal.msgSuccess("新增成功");
              }
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除节目编号为"' + ids + '"的数据项？').then(function() {
        return delHgitems(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
<style>
.scrollable-content {
  max-height: 80vh;   /* 或固定像素值如 600px */
  overflow-y: scroll;  /* 改为scroll，始终显示滚动条 */
  padding-right: 10px; /* 避免滚动条遮挡内容 */
}
.scrollable-content::-webkit-scrollbar {
  width: 8px; /* 滚动条宽度 */
}
.scrollable-content::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道颜色 */
  border-radius: 4px;
}
.scrollable-content::-webkit-scrollbar-thumb {
  background: #c0c0c0; /* 滚动条滑块颜色 */
  border-radius: 4px;
}
.scrollable-content::-webkit-scrollbar-thumb:hover {
  background: #a0a0a0; /* 滚动条滑块悬停颜色 */
}
</style>
